#define _CRT_SECURE_NO_WARNINGS  1
#include<stdio.h>
#include<stdlib.h>
typedef struct Lnode
{
	int data;
	struct Lnode* next;
}Lnode;

void creat(Lnode* L, int* arr,int sz)
{
	Lnode* end = L;
	for (int i = 0; i < sz; i++)
	{
		Lnode* node = (Lnode*)malloc(sizeof(Lnode));
		node->data = arr[i];
		end->next = node;
		end = node;
	}
	end->next = NULL;
}

void  creat1(Lnode* L, int* arr, int sz)
{
	Lnode* end = L;
	for (int i = sz-1; i >= 0; i--)
	{
		Lnode*node = (Lnode*)malloc(sizeof(Lnode));
		node->data = arr[i];
		node->next = end->next;
		end->next = node;
	}
	

}

void pushback(Lnode* L)
{
	Lnode* end = L,*node = (Lnode*)malloc(sizeof(Lnode));
	while (end->next!=NULL)
	{
		end = end->next;
	}
	end->next = node;
	node->data = 10;


	node->next = NULL;
}

void pushfront(Lnode* L)
{
	Lnode*node= (Lnode*)malloc(sizeof(Lnode));
	node->data = 0;
	node->next = L->next;
	L->next = node;
}

void popfront(Lnode* L)
{
	Lnode* p = L->next;
	Lnode* s = p->next;
	free(p);
	L->next = s;

}

void popback(Lnode* L)
{
	Lnode* p = L->next;
	while (p->next->next)
	{
		p = p->next;
	}
	free(p->next);
	p->next = NULL;
}

void insert(Lnode* L, int n,int x)
{
	Lnode* p = L->next;
	Lnode* node = (Lnode*)malloc(sizeof(Lnode));
	while (p->next->data != n)
	{
		p = p->next;
	}
	node->next = p->next;
	p->next = node;
	node->data = x;
}

void afterinsert(Lnode* L, int n, int x)
{
	Lnode* p = L->next;
	Lnode* node = (Lnode*)malloc(sizeof(Lnode));
	while (p->data != n)
	{
		p = p->next;
	}
	node->next = p->next;
	p->next = node;
	node->data = x;
	
}

void destroy(Lnode* L)
{
	Lnode* p = L;
	while (p)
	{
		Lnode*s = p->next;	
		free(p);
		p = s;
	}
	L->next = NULL;

}
int main()
{
	Lnode* L = (Lnode*)malloc(sizeof(Lnode));
	L->next = NULL;
	int arr[] = { 1,2,3,4,5,6,7,8,9};
	int sz = sizeof(arr) / sizeof(arr[0]);
	//creat(L,arr,sz);
	creat1(L,arr,sz);
	pushback(L);
	pushfront(L);
	popfront(L);
	popback(L);
	insert(L,3,88);
	afterinsert(L, 3, 99);
	destroy(L);
	Lnode* p = L->next;
	while(p!=NULL)
	{
		printf("%d ", p->data);
		p = p->next;
		
	}
}